package Draw0513;

import java.awt.*;
import java.util.ArrayList;

public class SierpinskiShape extends ShapeV2{



    public SierpinskiShape(int x1, int y1, int x2, int y2, Color color, String type) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.color = color;
        this.type = type;
    }

    public SierpinskiShape(int x3, int y3, int x4, int y4, int x5, int y5,Color color, String type) {
        this.x3 = x3;
        this.y3 = y3;
        this.x4 = x4;
        this.y4 = y4;
        this.x5 = x5;
        this.y5 = y5;
        this.color = color;
        this.type = type;
    }

    ArrayList<Point> points ;

    public void addPoint(Point point){
        points.add(point);
    }


    public void drawSierpinskiRect(int x, int y, int w, int h, int n, Graphics g){
        if (n <= 0){
            return;
        }
        n--;
        g.fillRect(x + w / 3, y + h / 3, w / 3, h / 3);

        drawSierpinskiRect(x, y, w / 3, h / 3,n, g);
        drawSierpinskiRect(x + w / 3, y, w / 3, h / 3,n,g);
        drawSierpinskiRect(x + 2 * w / 3, y, w / 3, h / 3,n,g);


        drawSierpinskiRect(x, y + h / 3, w / 3, h / 3,n,g);
        drawSierpinskiRect(x + 2 * w / 3, y + h / 3, w / 3, h / 3,n,g);


        drawSierpinskiRect(x, y + 2 * h / 3, w / 3, h / 3,n,g);
        drawSierpinskiRect(x + w / 3, y + 2 * h / 3, w / 3, h / 3,n,g);
        drawSierpinskiRect(x + 2 * w / 3, y + 2 * h / 3, w / 3, h / 3,n,g);
    }

    public void drawSierpinskiRound(int x, int y, int w, int h, int n, Graphics g){
        if (n <= 0){
            return;
        }
        n--;
        g.fillOval(x + w / 4, y + h / 4, w / 2, h / 2);

        drawSierpinskiRound(x + w / 4, y, w / 2, h / 2,n,g);

        drawSierpinskiRound(x, y + h / 4, w / 2, h / 2,n,g);
        drawSierpinskiRound(x + 3 * w / 4, y + h / 4, w / 2, h / 2,n,g);

        drawSierpinskiRound(x + w / 4, y + 3 * h / 4, w / 2, h / 2,n,g);
    }

    public void drawSierpinskiPolyg(int x3, int y3, int x4, int y4, int x5, int y5, int n, Graphics g){
        if (n <= 0){
            return;
        }
        n--;
        Polygon pg1 = new Polygon();
        pg1.addPoint((x3+x4)/2,(y3+y4)/2);
        pg1.addPoint((x5+x4)/2,(y5+y4)/2);
        pg1.addPoint((x3+x5)/2,(y3+y5)/2);

        g.fillPolygon(pg1);


        drawSierpinskiPolyg(x3,y3,(x3+x4)/2,(y3+y4)/2,(x3+x5)/2,(y3+y5)/2,n,g);
        drawSierpinskiPolyg(x4,y4,(x3+x4)/2,(y3+y4)/2,(x4+x5)/2,(y4+y5)/2,n,g);
        drawSierpinskiPolyg(x5,y5,(x5+x4)/2,(y5+y4)/2,(x3+x5)/2,(y3+y5)/2,n,g);
    }

    @Override
    public void drawShapeV2(Graphics g) {
        if (type.equals("谢尔宾斯基矩形")){
            g.drawRect(Math.min(x1,x2), Math.min(y1,y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
            drawSierpinskiRect(Math.min(x1,x2), Math.min(y1,y2), Math.abs(x1 - x2), Math.abs(y1 - y2), 5, g);
        }else if (type.equals("谢尔宾斯基三角形")){
           drawSierpinskiPolyg(x3,y3,x4,y4,x5,y5,5,g);
        }else if (type.equals("谢尔宾斯基圆形")){

        }

    }
}
